<?php

/**
 *		//定义一个从主数据库查询的模块//
 *		//这是一份试验品，地宝网5毛管理系统，由大街编写（zhongmingzi@gmail.com）
 *		//本程序默认环境：Zend Optimizer Yes / 3.3.3
 *		//		Apache/2.0.63 (Win32) PHP/5.2.12
 *		//		MySQL 支持 Yes / client lib version 5.0.89
 *		//		GD library Yes / bundled (2.0.34 compatible)
 *		//		eAccelerator/Memcache/xcache Yes
 *		//		cURL support enabled
 *		//时间：2011-02-08//
 */

class api {

	function api() {
	}

	function test($get, $post) {
		return API_RETURN_SUCCEED;
	}

	function verifyuser($data) {
		global $_G;
		if(!API_VERIFYUSER) {
			return API_RETURN_FORBIDDEN;
		}
		$data['password'] = md5($data['password']);
		$v = DB::fetch_first("SELECT * FROM ".DB::table('members')." WHERE username = '{$data['name']}' AND password = '{$data['password']}'");
		if(!empty($v)) return $v;
		else return API_RETURN_FAILED;
	}
	
	//这里最好给posts建立一个并列索引，tid，authorid
	function viewpost($data) {
		global $_G;
		if(!API_VIEWPOST) {
			return API_RETURN_FORBIDDEN;
		}
		$sql = "SELECT * FROM ".DB::table('posts')." WHERE tid = {$data['tid']} AND authorid IN ( {$data['authorid']}) ORDER BY pid DESC ";
		$f = prepage($data['page'], $data['limit'], $sql);
		$return['ptotal'] = $f['ptotal'];
		$return['total'] = $f['total'];
		$sql .= "LIMIT {$f['offset']}, {$f['limit']}";
		$q = DB::query($sql);
		while($v = DB::fetch($q)) {
			$return['data'][] = $v;
		}
		return $return;
	}
	
	function countpost($data) {
		global $_G;
		if(!API_COUNTPOST) {
			return API_RETURN_FORBIDDEN;
		}
		$sql = "SELECT COUNT(pid) FROM ".DB::table('posts')." WHERE authorid IN ({$data['windid']})";
		return DB::result_first($sql);
	}

	//同步登陆功能，暂时保留
	function synlogin($get, $post) {
		global $_G;

		if(!API_SYNLOGIN) {
			return API_RETURN_FORBIDDEN;
		}

		header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');

		$cookietime = 31536000;
		$uid = intval($get['uid']);
		$query = DB::query("SELECT uid, username, password FROM ".DB::table('members')." WHERE uid='$uid'");
		if($member = DB::fetch($query)) {
			dsetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
		}
	}

	function synlogout($get, $post) {
		global $_G;

		if(!API_SYNLOGOUT) {
			return API_RETURN_FORBIDDEN;
		}

		header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');

		dsetcookie('auth', '', -31536000);
	}
}


?>
